home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Misc / msql-1.0.6 / src / msql / msqladmin.c < prev    next >
C/C++ Source or Header  |  1995-02-14  |  5KB  |  247 lines

  1. /*
  2. **    msqladmin.c    - 
  3. **
  4. **
  5. ** Copyright (c) 1993  David J. Hughes
  6. **
  7. ** Permission to use, copy, and distribute for non-commercial purposes,
  8. ** is hereby granted without fee, providing that the above copyright
  9. ** notice appear in all copies and that both the copyright notice and this
  10. ** permission notice appear in supporting documentation.
  11. **
  12. ** This software is provided "as is" without any expressed or implied warranty.
  13. **
  14. ** ID = "$Id:"
  15. **
  16. */
  17.  
  18.  
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include <fcntl.h>
  22.  
  23. #ifdef HAVE_DIRENT
  24. #  include <dirent.h>
  25. #else
  26. #  include <sys/dir.h>
  27. #endif
  28.  
  29. #include "version.h"
  30. #include "msql.h"
  31.  
  32.  
  33. #include <common/site.h>
  34. #include <common/portability.h>
  35.  
  36. int    qFlag = 0;
  37. char    *msqlHomeDir;
  38.  
  39.  
  40. usage()
  41. {
  42.     printf("\n\nusage : msqladmin [-h host] [-q] <Command>\n\n");
  43.     printf("where command =");
  44.     printf("\t drop DatabaseName\n");
  45.     printf("\t\t create DatabaseName\n");
  46.     printf("\t\t shutdown\n");
  47.     printf("\t\t reload\n");
  48.     printf("\t\t version\n");
  49.     printf("\n -q\tQuiet mode.  No verification of commands.\n\n");
  50. }
  51.  
  52.  
  53. createDB(sock,db)
  54.     int    sock;
  55.     char    *db;
  56. {
  57.     if(msqlCreateDB(sock,db) < 0)
  58.     {
  59.         fprintf(stderr,"\nmSQL Command failed!\nServer error = %s\n\n",
  60.                 msqlErrMsg);
  61.         msqlClose(sock);
  62.         exit(1);
  63.     }
  64.     else
  65.     {
  66.         printf("Database \"%s\" created.\n",db);
  67.     }
  68. }
  69.  
  70.  
  71. dropDB(sock,db)
  72.     int    sock;
  73.     char    *db;
  74. {
  75.     char    buf[10];
  76.  
  77.  
  78.     if (!qFlag)
  79.     {
  80.         printf("\n\nDropping the database is potentially a very bad ");
  81.         printf("thing to do.\nAny data stored in the database will be");
  82.         printf(" destroyed.\n\nDo you really want to drop the ");
  83.         printf("\"%s\" ",db);
  84.         printf("database?  [Y/N] ");
  85.         bzero(buf,10);
  86.         fgets(buf,10,stdin);
  87.         if ( (*buf != 'y') && (*buf != 'Y'))
  88.         {
  89.             printf("\n\nOK, aborting database drop!\n\n");
  90.             msqlClose(sock);
  91.             exit(0);
  92.         }
  93.     }
  94.     if(msqlDropDB(sock,db) < 0)
  95.     {
  96.         fprintf(stderr,"\nmSQL Command failed!\nServer error = %s\n\n",
  97.             msqlErrMsg);
  98.         msqlClose(sock);
  99.         exit(1);
  100.     }
  101.     else
  102.     {
  103.         fprintf(stderr,"Database \"%s\" dropped\n",db);
  104.     }
  105. }
  106.  
  107.  
  108.  
  109. main(argc,argv)
  110.     int    argc;
  111.     char    *argv[];
  112. {
  113.     int    sock,
  114.         c,
  115.         argsLeft,
  116.         errFlag = 0;
  117.     char    *host = NULL;
  118.     extern    int optind;
  119.     extern    char *optarg;
  120.  
  121.  
  122.         msqlHomeDir = (char *)getenv("MSQL_HOME");
  123.         if (!msqlHomeDir)
  124.         {
  125.                 msqlHomeDir = INST_DIR;
  126.         }
  127.  
  128.     while((c=getopt(argc,argv,"h:q"))!= -1)
  129.         {
  130.                 switch(c)
  131.                 {
  132.                         case 'h':
  133.                                 if (host)
  134.                                         errFlag++;
  135.                                 else
  136.                                         host = optarg;
  137.                                 break;
  138.             case 'q':
  139.                 if (qFlag)
  140.                     errFlag++;
  141.                 else
  142.                     qFlag++;
  143.                 break;
  144.             case '?':
  145.                 errFlag++;
  146.                 break;
  147.         }
  148.     }
  149.  
  150.     argsLeft = argc - optind;
  151.  
  152.     if (errFlag || argsLeft == 0)
  153.     {
  154.         usage();
  155.         exit(1);
  156.     }
  157.  
  158.  
  159.         if ((sock = msqlConnect(host)) < 0)
  160.         {
  161.                 fprintf(stderr,"ERROR : %s\n",msqlErrMsg);
  162.                 exit(1);
  163.         }
  164.  
  165.     if (strcmp(argv[optind],"create") == 0)
  166.     {
  167.         if (argsLeft != 2)
  168.         {
  169.             usage();
  170.             msqlClose(sock);
  171.             exit(1);
  172.         }
  173.         createDB(sock,argv[optind+1]);
  174.         msqlClose(sock);
  175.         exit(0);
  176.     }
  177.     if (strcmp(argv[optind],"drop") == 0)
  178.     {
  179.         if (argsLeft != 2)
  180.         {
  181.             usage();
  182.             msqlClose(sock);
  183.             exit(1);
  184.         }
  185.         dropDB(sock,argv[optind+1]);
  186.         msqlClose(sock);
  187.         exit(0);
  188.     }
  189.     if (strcmp(argv[optind],"shutdown") == 0)
  190.     {
  191.         if (argsLeft != 1)
  192.         {
  193.             usage();
  194.             msqlClose(sock);
  195.             exit(1);
  196.         }
  197.         if(msqlShutdown(sock) < 0)
  198.         {
  199.             printf("\nmSQL Command failed!\nServer error = %s\n\n",
  200.                 msqlErrMsg);
  201.             msqlClose(sock);
  202.             exit(1);
  203.         }
  204.         exit(0);
  205.     }
  206.     if (strcmp(argv[optind],"reload") == 0)
  207.     {
  208.         if (argsLeft != 1)
  209.         {
  210.             usage();
  211.             msqlClose(sock);
  212.             exit(1);
  213.         }
  214.         if(msqlReloadAcls(sock) < 0)
  215.         {
  216.             printf("\nmSQL Command failed!\nServer error = %s\n\n",
  217.                 msqlErrMsg);
  218.             msqlClose(sock);
  219.             exit(1);
  220.         }
  221.         msqlClose(sock);
  222.         exit(0);
  223.     }
  224.     if (strcmp(argv[optind],"version") == 0)
  225.     {
  226.         if (argsLeft != 1)
  227.         {
  228.             usage();
  229.             msqlClose(sock);
  230.             exit(1);
  231.         }
  232.         printf("\nVersion Details :-\n\n");
  233.         printf("\tmsqladmin version \t%s\n",SERVER_VERSION);
  234.         printf("\tmSQL connection \t%s\n",msqlGetHostInfo());
  235.         printf("\tmSQL server version \t%s\n", msqlGetServerInfo());
  236.         printf("\tmSQL protocol version \t%d\n", msqlGetProtoInfo());
  237.         printf("\tmSQL TCP socket \t%d\n", MSQL_PORT);
  238.         printf("\tmSQL UNIX socket \t%s\n", MSQL_UNIX_ADDR);
  239.         printf("\tmSQL root user \t\t%s\n", ROOT);
  240.         msqlClose(sock);
  241.         exit(0);
  242.     }
  243.     usage();
  244.     msqlClose(sock);
  245.     exit(1);
  246. }
  247.